package com.example.myapplication.shortvideo.view;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import androidx.annotation.NonNull;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.shortvideo.utils.DateUtils;

/**
 * @author: rongfeng
 * @date: 2020/7/14
 * @description 视频拖动进度条
 */
public class VideoSeekBarView extends RelativeLayout {
    public interface VideoSeekBarCallback {
        int getCurrentTime();

        void setSeekTime(long time);
    }

    private Context mContext;
    private View mView;
    private SeekBar mSeekBar;
    private LinearLayout mLl_seek_time;
    private TextView tv_progress_time, tv_total_time;
    private VideoSeekBarCallback mVideoSeekBarCallback;
    private int mTotalTime;

    public VideoSeekBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mView = LayoutInflater.from(mContext).inflate(R.layout.view_short_video_seek_bar, this);
        mSeekBar = mView.findViewById(R.id.seekBar);
        mLl_seek_time = mView.findViewById(R.id.ll_seek_time);
        tv_progress_time = mView.findViewById(R.id.tv_progress_time);
        tv_total_time = mView.findViewById(R.id.tv_total_time);
        setVisibility(GONE);
        init();
    }

    private void init() {
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                String formatTime1 = DateUtils.formatSeconds(mVideoSeekBarCallback.getCurrentTime(), "mm:ss");
                tv_progress_time.setText(formatTime1);
                mSeekBar.getThumb().setAlpha(255);
                mLl_seek_time.setVisibility(View.VISIBLE);
                mHandler.removeMessages(1);
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mHandler.sendEmptyMessageDelayed(1, 3000);
                mLl_seek_time.setVisibility(View.GONE);
                mVideoSeekBarCallback.setSeekTime(mTotalTime / 1000 * seekBar.getProgress());
            }
        });
    }

    public void setCallback(VideoSeekBarCallback callback) {
        mVideoSeekBarCallback = callback;
    }

    public void setVideoTotalTime(int totalTime) {
        mTotalTime = totalTime;
        String formatTime2 = DateUtils.formatSeconds(totalTime, "mm:ss");
        tv_total_time.setText(formatTime2);
    }


    Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                mSeekBar.getThumb().setAlpha(0);
            }
        }
    };

    public void clear() {
        mHandler.removeMessages(1);
    }

}
